<template>
    <div class="hc-deliver-subject-entrance">
        <span class="hc-deliver-subject-entrance__guide-text">请选择科目:</span>
        <div class="hc-deliver-subject-entrance__subject-list" v-loading="loading">
            <div v-for="subject in subjectList"
                 class="hc-deliver-subject-entrance__subject-list-item"
                 @click="enterSectionSeletion(subject.node)">
                <div>
                    <img :src="require('../assets/subjectSelect/'+ matchingIcon(subject.nodeTitle) + '.png')">
                </div>
                <div class="hc-deliver-subject-entrance__subject-name">{{subject.nodeTitle}}</div>
            </div>
        </div>
    </div>
</template>

<script>
    import {ConstJsonManager} from '../utils/ConstJsonManager'
    import {mutations_const, actions_const} from '../config/StoreConstant'
    // import {addDevice} from "../utils/WebsocketConfig";

    export default {
        name: 'selectSubject',
        data() {
            return {
                subjectList: [],
                loading: true
            }
        },
        created() {
            this.init();
        },
        computed: {},
        methods: {
            init() {
                this.$store.dispatch(actions_const.GET_LOGIN_MEMBER).then(() => {
                    this.$store.dispatch(actions_const.GET_CLASS_INFO).then(() => {
                        Promise.all([
                            this.$store.dispatch(actions_const.GET_CLASS_DEVICE_LIST),
                            this.$store.dispatch(actions_const.GET_CLASS_MEMBER),
                            this.$store.dispatch(actions_const.GET_CLASS_TEACHING_ASSISTANT),
                        ]).then(() => {
                            this.getSubject();
                        }).catch(() => {
                            this.init();
                        })
                    })
                }).catch(() => {
                    vueMain.$store.commit(mutations_const.RESET_LOGIN_MEMBER, {})
                    vueMain.$store.commit(mutations_const.RESET_CLASS, {
                        info: {},
                        member: {},
                        teachingAssistant: {},
                        device: {
                            groupList: [],
                            list: []
                        },
                    })
                    Promise.all([
                        ConstJsonManager.set('token', ''),
                        // ConstJsonManager.set('autoLogin', false)
                        ConstJsonManager.set('rememberPass', false)
                    ])
                        .then(() => {
                            vueMain.$router.push('/login')
                        })
                })
            },
            enterSectionSeletion(node) {
                this.$store.commit('UPD_SUBJECT', node);
                this.$router.push({name: 'select-section'});
            },
            getSubject() {
                let subjectList = this.$store.getters.getSubjectList;
                for (let subject in subjectList) {
                    if (subjectList.hasOwnProperty(subject)) {
                        let temp = subjectList[subject];
                        this.subjectList.push(temp);
                    }
                }
                this.loading = false;
            },
            matchingIcon(title) {
                if (!title) return 'biology';
                if (title.indexOf('语文') !== -1) return 'chinese';
                if (title.indexOf('数学') !== -1) return 'math';
                if (title.indexOf('英语') !== -1) return 'english';
                if (title.indexOf('物理') !== -1) return 'physics';
                if (title.indexOf('化学') !== -1) return 'chemistry';
                if (title.indexOf('生物') !== -1) return 'biology';
                if (title.indexOf('政') !== -1 || title.indexOf('思') !== -1 || title.indexOf('德') !== -1) return 'politics';
                if (title.indexOf('史') !== -1) return 'history';
                if (title.indexOf('地') !== -1) return 'geography';
                return 'biology';
            }
        },
        beforeRouteEnter(to, from, next) {
            next(vm => {
                if (from.name === 'login' && to.name === 'select-subject') {
                    // if (vm.$store.state.Counter.hasAddDevice !== vm.$store.state.Member.loginMember.id) {
                    //     try {
                    //         addDevice();
                    //     } catch(e) {
                    //         console.log(e)
                    //     }
                    // }
                }
            })
        }
    }
</script>
